package com.omg.server.framework.ibatis;

public class OracleDialect implements Dialect{

	private static final String SQL_END_DELIMITER = ";";  

	/**
	 * 获取oracle分页SQL
	 */
	@Override
	public String getPageSql(String sql, int offset, int limit) {
		
        if (offset == 1) {  
	        offset = 0;  
	    }  
        
        StringBuffer pageStr = new StringBuffer();  
        pageStr.append("select * from ( select row_limit.*, rownum rn from (");  
        pageStr.append(this.removeSemiColon(sql));  
        pageStr.append(" ) row_limit where rownum <= ");  
        pageStr.append(limit + offset);  
        pageStr.append(" ) where rn >");  
        pageStr.append(offset);  
        return pageStr.toString();  

	}
	
	/** 
     * 去掉当前SQL 后分号 
     * @param sql 
     * @return 
     */  
    private String removeSemiColon(String sql) {
    	if (null == sql){
    	    return "";
    	}
    	
        sql = sql.trim();  
        if (sql.endsWith(SQL_END_DELIMITER)) {  
            sql = sql.substring(0, sql.length() - 1  - SQL_END_DELIMITER.length());  
        }  
        return sql;  
    }

}
